package com.lin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lin.reggie.Dto.SetmealDto;
import com.lin.reggie.common.CustomException;
import com.lin.reggie.entity.Dish;
import com.lin.reggie.entity.Setmeal;
import com.lin.reggie.entity.SetmealDish;
import com.lin.reggie.mapper.SetmealMapper;
import com.lin.reggie.service.SetmealDishService;
import com.lin.reggie.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;


    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        List<SetmealDish> setmealDishes =setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId().toString());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void deleteWithSetmealDish(List<Long> id) {
        //查询套餐的状态，是否可以删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();

        setmealLambdaQueryWrapper.in(Setmeal::getId,id);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        //如果不能删除，则抛出一个异常
        int count = this.count(setmealLambdaQueryWrapper);
        if (count >0){
            throw new CustomException("存在套餐真在售卖中，不可删除");
        }
        //如果可以删除，则先删除套餐表中的数据
        this.removeByIds(id);
        //删除关系表中的数据，即套餐中菜品的套餐菜品
        //delete form setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,id);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

    }

    @Override
    public void updateSetmealStatusById(Integer status,List<Long> ids) {

        //条件构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(ids!=null,Setmeal::getId,ids);
        List<Setmeal> list = this.list(setmealLambdaQueryWrapper);
        for (Setmeal setmeal :list){
            if (setmeal!=null){
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }

    @Override
    public SetmealDto getData(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> setmealDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //在关联表中查询套餐内的菜品
        setmealDtoLambdaQueryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);

        if (setmeal!=null){
            BeanUtils.copyProperties(setmeal,setmealDto);
            List<SetmealDish> list = setmealDishService.list(setmealDtoLambdaQueryWrapper);
            setmealDto.setSetmealDishes(list);
            return setmealDto;
        }
        return null;
    }


}
